//
//  CZStatusLabel.swift
//  weibo
//
//  Created by Apple on 16/3/22.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class CZStatusLabel: UILabel {
    //  富文本匹配的结果
    var matchResults: [CZMatchResult]?
    //  当前点击的范围
    var currentRange: NSRange?
    
    //  保存高亮视图
    private lazy var bgViews: [UIView] = [UIView]()
    
    override var attributedText: NSAttributedString? {
        didSet {
            textView.attributedText = attributedText
        }
    }
    
    private lazy var textView: UITextView = {
        let textView = UITextView()

        //  关闭用户交互,就不会弹框了
        textView.userInteractionEnabled = false
        //  设置不可编辑，这局代码可以不设置
        textView.editable = false
        //  设置不可滚动
        textView.scrollEnabled = false
        textView.font = UIFont.systemFontOfSize(15)
        //  设置文本框透明要不然我添加的label不可见
        textView.alpha = 0
        //  设置内容间距
        textView.textContainerInset = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: -5)
        return textView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI() {
        userInteractionEnabled = true
        //  添加控件
        addSubview(textView)
        //  设置约束
        textView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let location = touches.first!.locationInView(self)
        print(location)
        //  获取到手指点击位置的字符 range
        let textRange = textView.characterRangeAtPoint(location)
        print(textRange)
        //  通过设置selectedTextRange
        textView.selectedTextRange = textRange
        //  获取选中的范围
        let range = textView.selectedRange
        
        if matchResults != nil {
            
            for matchResult in matchResults! {
               //   查找点击的位置是否在我们传入的匹配结果中
                if NSLocationInRange(range.location, matchResult.range) {
                    
                    //  设置selectedRange，为了使用selectedTextRange
                    textView.selectedRange = matchResult.range
                    //  通过selectedTextRange获取选中的区域,返回的数组里面存放的是UITextSelectionRect
                    let sectionRects = textView.selectionRectsForRange(textView.selectedTextRange!)
                    //  遍历区域范围
                    for value in sectionRects {
                        //  把数据强转成UITextSelectionRect
                        let rect = value as! UITextSelectionRect
                        //  设置视图
                        let bgView = UIView(frame: rect.rect)
                        bgView.backgroundColor = RGB(177, green: 215, blue: 255)
                        bgView.layer.cornerRadius = 3
                        bgView.layer.masksToBounds = true
                        //  添加在最下面
                        insertSubview(bgView, atIndex: 0)
                        
                        bgViews.append(bgView)
                    }
                    
                }
                
            }
        }
        
        
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touchesCancelled(touches, withEvent: event)
    }
    
    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        //  移除高亮显示
        for value in bgViews {
            value.removeFromSuperview()
        }
        bgViews.removeAll()
    }
    
}


/*

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let location = touches.first!.locationInView(self)
print(location)
//  获取到手指点击位置的字符 range
let textRange = textView.characterRangeAtPoint(location)
print(textRange)
//  通过设置selectedTextRange
textView.selectedTextRange = textRange
//  获取选中的范围
let range = textView.selectedRange

if matchResults != nil {

for matchResult in matchResults! {
//   查找点击的位置是否在我们传入的匹配结果中
if NSLocationInRange(range.location, matchResult.range) {
//  设置选中范围的富文本
let attr = NSMutableAttributedString(attributedString: attributedText!)
attr.addAttribute(NSBackgroundColorAttributeName, value: UIColor.blueColor(), range: matchResult.range)

self.attributedText = attr
//  设置匹配的结果范围
currentRange = matchResult.range
}

}
}


}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//  移除高亮显示
if currentRange != nil {
let attr = NSMutableAttributedString(attributedString: attributedText!)
attr.removeAttribute(NSBackgroundColorAttributeName, range: currentRange!)

self.attributedText = attr

currentRange = nil
}




}


*/
